{% comment %}
Copyright (C) 2018 Google LLC.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.foobar2
{% endcomment %}

{% comment %}
This logic is trying to find the first portion of the path after the base url to match for
the top nav. The page url should be in the form /foo/bar/. Sometimes pages also have a trailing
.html like /foo/bar.html or a trailing /. We strip those off and split on the / separator
(the split function handles the trailing / and ignores it) to find the base "/foo" to figure out
what top nav subtree we are in.
{% endcomment %}
{% assign page_parts = page.url | remove_first: "/" | split: "/" %}
{% assign page_url = page_parts[0] | prepend: "/" | remove: ".html" %}

<nav class="c-navigation {% if site.fixedNav == 'true' %}is-fixed{% endif %}">
  <div class="c-navigation__container u-container">

    {% for i in site.nav %}
      {% assign url = i.item.url %}
      {% assign url_for_matching = url | remove: ".html" %}
      <a class="c-navigation__item {% if page_url == url_for_matching %}is-active{% endif %}"
         href="{{ url | prepend: site.baseurl }}">{{i.item.name}}</a>
    {% endfor %}

  </div>
</nav>
